from ..utils.utils import *
from ..myModels.orderModel import *
from ..myModels.preOrderModel import *
from ..myModels.cartModel import *
import requests
import json
import random

from drf_yasg.utils import swagger_auto_schema
from drf_yasg import openapi
from rest_framework.decorators import api_view
from rest_framework.response import Response

@api_view(['post'])
def order_add(request):
    try:
        # 创建订单
        address_info = get_param(request, 'addressInfo')
        express_fee = get_param(request, 'expressFee')
        payway = get_param(request, 'payway')
        all_fee = get_param(request, 'totalFee')
        discount = get_param(request, 'discount')
        pre_order_id = get_param(request, 'preOrderId')
        params = {
            "address_info": address_info,
            "express_fee": express_fee,
            "payway": payway,
            "all_fee": all_fee,
            "discount": discount,
            "pre_order_id": pre_order_id
        }
        params = change_key_to_line(params)
        order = Order.objects.create(**params)

        # 请求支付信息
        pay_data = {
            "orderId": order.id,
            "allFee": all_fee
        }
        url = 'http://81.71.65.4:3003/order/pay'
        res_pay = requests.post(url, data=pay_data)
        res_data = json.loads(res_pay.text)

        order.order_status = '0'+ str(random.randint(1, 3))
        order.qrcode = res_data['result']['qrcode']
        print(res_data['result'])
        order.company = '天堂电子商务有限公司'

        order.total_money = all_fee

        pre_order = PreOrder.objects.filter(id=pre_order_id).first()
        order.pre_order_id = pre_order.id
        order.user_id = pre_order.user_id
        order.cart_id = pre_order.cart_id
        order.save()

        return success(m_to_d(order))
    except Exception as e:
        return fail(str(e))

@api_view(['get'])
def order_all(request):
    try:
        user_id = request.session.get('session')
        if not user_id:
            return fail('未登录或者session已过期')
        res_list = Order.objects.filter(
            user_id=user_id).order_by('ctime').values()
        res_list = list(res_list)
        for item in res_list:
            ids = item['cart_id'].split(',')
            carts = Cart.objects.filter(id__in=ids).values()
            item['carts'] = change_key_for_list(list(carts))

        return success(res_list)
    except Exception as e:
        return fail(str(e))


def order_list(request):
    return success('list')

@api_view(['get'])
def order_detail(request): 
    try:
        order_id = get_param(request,'orderId')
        if not order_id:
            return fail('缺少orderId')
        order = Order.objects.filter(id=order_id).first()
        ids = order.cart_id.split(',')  
        
        carts = Cart.objects.filter(id__in=ids).values() 
        carts = change_key_for_list(list(carts))
        res_data = change_key_for_dict(m_to_d(order))
        res_data['carts'] = carts 
        return success(res_data,ids=ids)
    except Exception as e:
        return fail(str(e))
    

@api_view(['post'])
def order_update(request):
    return success('update')

@api_view(['post'])
def order_del(request):
    return success('del')
